package ndk.caro.views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ImageView;
import ndk.caro.activities.R;
import ndk.caro.activities.myCaroActivity;
import ndk.caro.activities.myMainActivity;
import ndk.caro.gameplays.myCaroGame;
import ndk.caro.gameplays.myCaroGame.Player;

public class myMenuView extends ImageView{	
	public int bkg_main;
	public int bkg_touch;	
	
	private int id;
	private final Activity actMain;
	private final Vibrator mVibrator;
	private final long LONG_VALIDATOR = 15;

	
	public myMenuView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.actMain = (Activity)context;			
		initBack();	
		this.setBackgroundDrawable(getResources().getDrawable(bkg_main));			
		mVibrator = (Vibrator)this.actMain.getSystemService(Context.VIBRATOR_SERVICE);
	}
	
	private void initBack() {
		id = getId();
		switch (id) {
		case R.id.menu_pvsblue:
			bkg_main = R.drawable.buttonbluetooth;
			bkg_touch = R.drawable.buttonbluetootho;
			break;
			
		case R.id.menu_pvscpu:
			bkg_main = R.drawable.buttonmenupvscpu;
			bkg_touch = R.drawable.buttonmenupvscpuo;
			break;
			
		case R.id.menu_p1vsp1:
			bkg_main = R.drawable.buttonmenupvsp;
			bkg_touch = R.drawable.buttonmenupvspo;
			break;
			
		case R.id.menu_author:
			bkg_main = R.drawable.buttonmenuauthor;
			bkg_touch = R.drawable.buttonmenuauthoro;
			break;
			
		case R.id.menu_exit:
			bkg_main = R.drawable.buttonmenuexit;
			bkg_touch = R.drawable.buttonmenuexito;
			break;
			
		case R.id.mnu_star:
			bkg_main = R.drawable.buttonstartgame;
			bkg_touch = R.drawable.buttonstartgameo;
			break;
			
		default:
			break;
		}
	}
		
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int actioncode = event.getAction();
		switch (actioncode) {
		case MotionEvent.ACTION_MOVE:		
			
		case MotionEvent.ACTION_DOWN:
			//Log.i("NDK", "ACTION_DOWN");
			this.setBackgroundDrawable(getResources().getDrawable(bkg_touch));
			this.mVibrator.vibrate(this.LONG_VALIDATOR);
			
			
			break;
		case MotionEvent.ACTION_UP:			
			//Log.i("NDK", "ACTION_UP");
			this.setBackgroundDrawable(getResources().getDrawable(bkg_main));
			switch (id) {
			case R.id.menu_pvsblue:
				//this.setPlayerForGame(Player.BLUE);
				break;
			case R.id.menu_pvscpu:
				//this.setPlayerForGame(Player.CPU);
				break;
			case R.id.menu_p1vsp1:
				this.setPlayerForGame(Player.PVSP);
				break;
				
			case R.id.menu_author:
				((myMainActivity)this.actMain).DisplayAuthor();
				break;
				
			case R.id.menu_exit:
				this.actMain.finish();
				break;
				
			case R.id.mnu_star:				
					((myCaroActivity)this.actMain).initNewGame();
					this.setVisibility(INVISIBLE);	
				break;
				
			default:
				break;
			}
		default:
			break;
		}
		return true;
	}
	
	public void setPlayerForGame(Player ePlayer) {
		this.actMain.startActivity(new Intent(this.actMain, myCaroActivity.class));				
		myCaroGame.setPlayer(ePlayer);		
	}	
}
